package com.toutou.base.util

import androidx.appcompat.app.AppCompatActivity

object ActivitiesManager {
    private val activities = mutableSetOf<AppCompatActivity>()

    fun addActivity(activity: AppCompatActivity) {
        activities.add(activity)
    }

    fun removeActivity(activity: AppCompatActivity) {
        activities.remove(activity)
    }

    fun topActivity(): AppCompatActivity? {
        return activities.lastOrNull()
    }

    fun clearOtherActivities(activity: AppCompatActivity) {
        activities.forEach {
            if (it != activity) {
                it.finish()
            }
        }
        activities.clear()
        activities.add(activity)
    }

    @Deprecated("Use clearOtherActivities instead")
    fun finishAllActivities() {
        activities.forEach {
            it.finish()
        }
        activities.clear()
    }
}